<?php
namespace plugins\store\model;
use think\Model;
use think\Db;

class ToolModel extends Model
{

    /**
     * [base64_image_content base64编码字符串 保存并生成图片链接]
     * @param  [type] $image [base64编码字符串]
     * @return [type]        [description]
     */
    public function base64_image_content($image,$str){
        if($image == base64_encode(base64_decode($image))){
            return $image;
        }
        $sui=mt_rand(10000,99999);
        $imageName = date('Ymd').'/'.$sui.date('His').".png"; //设置生成的图片名字
        if (strstr($image,",")){
            $image = explode(',',$image);
            $image = $image[1];
        }

        //获取网站路径
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
        $wzurl = $http_type.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
        $wzurl= substr($wzurl,0,strrpos($wzurl,'/'));

        //读取文件上传路径
        // $peizhi = getModuleConfig('evaluation','config','evaluateconfig.json');
        // $peizhi = json_decode($peizhi,true);
        // $path = './'.$peizhi['tu_path'];

        $this->uploadpath = ROOT_PATH.'public'.DS.'plugins'.DS.'store'.DS.'view'.DS.'image'.DS;
        $path =  $this->uploadpath.$str;

        // $path = "./upload/pingjia"; // 设置图片保存路径
        if (!is_dir($path.'/'.date('Ymd'))){ //判断目录是否存在 不存在就创建 并赋予777权限
            mkdir($path.'/'.date('Ymd'),0777,true);
        }

        $imageSrc= $path."/". $imageName; //图片名字
        $r = file_put_contents($imageSrc, base64_decode($image));//返回的是字节数
        if (!$r) {
            return zy_json_echo(error,'图片生成失败',null,-1);
        }else{
            // $pic = explode('.',$imageSrc,2)['1'];
            return $str."/".$imageName;
        }
    }	

}

